﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Portal.Web.Model;
using Portal.Cms.Model;
using Portal.Cms.Bussiness;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Cms.Utility;

namespace Portal.Cms.Render
{
    public class ApprovedDataRenderBll
    {
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideDownloadAttachFile(string SiteId, string DataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(null, SiteId, DataId);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                OAjaxOut.RetUrl = OCmsData.GetAttachDocUrl(SiteId,WebConfig.HttpDataRoot(OSiteParam));
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        public static string BaseSiteUrl
        {
            get
            {
                HttpContext context = HttpContext.Current;
                string baseUrl = context.Request.Url.Authority + context.Request.ApplicationPath.TrimEnd('/');
                return baseUrl;
            }
        }

        public static string ConvertUrl(string Url)
        {
            Url = Url.Replace("192.168.1.2/", BaseSiteUrl + "/");
            Url = Url.Replace("117.3.4.207/", BaseSiteUrl + "/");
            return Url;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSidePublic(string SiteId, string DataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).PublicData(null, SiteId, DataId);
                
                OAjaxOut.HtmlContent =
                    "<input type=button class=button  value=\"Ngưng phát hành\" onclick=\"javascript:ClientUnPublic('" + DataId + "');\">";
                OAjaxOut.InfoMessage = "Đã phát hành";
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideUnPublic(string SiteId, string DataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).UnPublicData(null, SiteId, DataId);
                
                OAjaxOut.HtmlContent =
                    "<input type=button class=button value=\"Phát hành\" onclick=\"javascript:ClientPublic('" + DataId + "');\">";
                OAjaxOut.InfoMessage = "Chưa phát hành";
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }

        ////////////////////////////// DOCUMENT //////////////
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string gid = (string)WebEnvironments.Request(SiteId, "gid");
                if (string.IsNullOrEmpty(gid)) throw new Exception("Chưa xác định Alias của nhóm tin!");
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, gid);
                if (OGroupData == null) throw new Exception("Dữ liệu không hợp lệ!");
                SiteParam OSiteParam=WebEnvironments.CreateSiteParam(SiteId);
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcAccess, gid, WebSessionUtility.GetCurrentLoginUser().UserId, true);
                //bool AllowPublish = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcPublish, gid, WebSessionUtility.GetCurrentLoginUser().UserId, false);

                string AssetCode = WebConfig.GetConfigFromXml(OSiteParam, "DefaultAssetCode");
                
                string Html =
                    " <script language=javascript>\r\n" +
                    "   function ProcessDownload(DocumentId)\r\n"+
                    "   {\r\n"+
                    "       SiteId='"+SiteId+"';\r\n"+
                    "       OAjaxOut=Portal.Cms.Render.ApprovedDataRenderBll.ServerSideDownloadDocument(SiteId,DocumentId).value;\r\n" +
                    "       if(OAjaxOut.Error)\r\n"+
                    "       {\r\n"+
                    "           alert(OAjaxOut.InfoMessage);\r\n"+
                    "           return;\r\n"+
                    "       }\r\n"+
                    "       window.open(OAjaxOut.RetUrl,'_blank');\r\n"+
                    "   }\r\n"+

                   "    function ClientPublic(CmsDataId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        if(confirm('Bạn đã chắc chắn phát hành chưa?')==false)return;\r\n" +
                   "        AjaxOut = Portal.Cms.Render.ApprovedDataRenderBll.ServerSidePublic(SiteId,CmsDataId).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +

                   "        document.getElementById('tr'+CmsDataId).style.display='none';\r\n" +
                   "        alert('Đã phát hành văn bản lên website!');\r\n"+
                   "    }\r\n" +
                 
                    " </script>\r\n";


                CmsDataCls[] CmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingCmsDataForPublish(SiteId, gid);
                Html+=
                   " <div class=FormFunction id=divListForm>\r\n" +
                   "    <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   //"            <li onclick=\"javascript:ClientAdd();\"><img class=image src=\"Resources/Images/Add.png\"><div>Tạo mới</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('RootApprovedData.aspx','_self');\"><img class=image src=\"Resources/Images/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "    </div>\r\n" +

                    " <h3>Duyệt phát hành - " + OGroupData.GroupDataName + "</h3>\r\n" +
                    " <div class=CssNewsHeading>Tổng số có " + CmsData.Length + " "+OGroupData.GroupDataName+" cần duyệt</div>\r\n" +
                    " <div class=CssResultItems>\r\n"+
                    " <table> \r\n";
                for (int iIndex = 0; iIndex < CmsData.Length; iIndex++)
                {

                    string PreviewImage = "Resources/Images/preview.png";
                    if (CmsData[iIndex].AttachedSearchable == 0)
                    {
                        PreviewImage = "Resources/Images/PreviewJpg.png";
                    }
                    
                    string PdfViewerUrl = WebConfig.GetConfigFromXml(OSiteParam, "PdfViewerUrl");
                    Html +=
                        " <tr id=\"tr"+CmsData[iIndex].DataId+"\"> \r\n" +
                        //"    <td style=\"width:40px;vertical-align:top\">" + NewImageText + "</td> \r\n" +
                        "    <td> \r\n" +
                        "        <div class=NewsTitle><a href=\"vbnbdocdetail.aspx?gid=" + OGroupData.GroupDataId + "&id=" + CmsData[iIndex].DataId + "&back=ApprovedData.aspx\">" + (iIndex + 1) + ". " + CmsData[iIndex].Subject + "</a></div> \r\n" +
                        "        <div><span class=itemheading>Số</span>: <span class=DocNo>" + CmsData[iIndex].DataAlias + "</span></div> \r\n" +
                        "        <div><span class=itemheading>Loại văn bản</span>: <span class=ReleaseFrom>" + CmsData[iIndex].DocTypeName + "</span></div> \r\n" +
                        "        <div><span class=itemheading>Đơn vị ban hành</span>: <span class=ReleaseFrom>" + CmsData[iIndex].DocReleaseFromName + "</span></div> \r\n" +
                        "        <div><span class=itemheading>Người ký</span>: <span class=ReleaseFrom>" + CmsData[iIndex].DocSignerName + "</span></div> \r\n" +
                        //"        <div><span class=itemheading>Ngày ký</span>: <span class=CssDate>" + CmsData[iIndex].SignDate + "</span> | Ban hành ngày <span class=CssDate>" + CmsData[iIndex].ReleaseDate + "</span> | Hiệu lực <span class=CssDate>" + CmsData[iIndex].EffectedDate + "</span></div> \r\n" +
                        "        <div>Ban hành ngày <span class=CssDate>" + CmsData[iIndex].ReleaseDate + "</span> | Hiệu lực <span class=CssDate>" + CmsData[iIndex].EffectedDate + "</span></div> \r\n" +
                        "        <div><span class=itemheading>Trích yếu</span>: <span class=CssTrichYeuDetail>" + CmsData[iIndex].Description + "</span></div> \r\n" +
                        "        <div class=download>\r\n" +
                        "           <table>\r\n" +
                        "               <tr>\r\n" +
                        "                   <td style=\"vertical-align:top\"><img class=image onclick=\"javascript:window.open('" + PdfViewerUrl + "?site=" + SiteId + "&id=" + CmsData[iIndex].DataId + "','_blank');\" style=\"border:none;\" alt=\"Xem tài liệu toàn văn\" title=\"Xem tài liệu toàn văn\" class=\"image\" src=\"" + PreviewImage + "\"><a href=\"javascript:ProcessDownload('" + CmsData[iIndex].DataId + "');\"><img alt=\"Download tài liệu gắn kèm\" title=\"Download tài liệu gắn kèm\" class=image src=\"Resources/Images/download.png\"></a><a href=\"javascript:ClientPublic('" + CmsData[iIndex].DataId + "');\"><img alt=\"Phát hành lên website\" title=\"Phát hành lên website\" class=image src=\"Resources/Images/publish.png\"></a></td>\r\n" +
                        "               </tr>\r\n"+
                        "           </table>\r\n"+
                        "       </div> \r\n" +
                        "   </td> \r\n" +
                        "</tr> \r\n";
                }
                Html +=
                    "</table>\r\n" +
                    "</div>\r\n"+
                    " <div class=\"clr\"></div>\r\n" +
                    " </div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public AjaxOut ServerSideDownloadDocument(string SiteId,string DocumentId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                CmsDataCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataModel(null, SiteId, DocumentId);
                OAjaxOut.RetUrl = OCmsData.GetAttachDocUrl(SiteId,WebConfig.HttpDataRoot(OSiteParam));
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }
    }
}
